/*!
 * @license snap-axis v1.3.0
 * Copyright (c) 2025 snap-axis nobo (MIT Licensed)
 * https://github.com/refline/snap-axis
 */
(function (global, factory) {
	typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
	typeof define === 'function' && define.amd ? define(factory) :
	(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.SnapAxis = factory());
})(this, (function () { 'use strict';

	var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};

	var umd_exports = {};

	var SnapAxis$1 = {};

	var helpers = {};

	Object.defineProperty(helpers, "__esModule", { value: true });
	helpers.isNil = helpers.isCloseEqual = helpers.hasAddSnapValue = void 0;
	function hasAddSnapValue(map, value) {
	    if (!map.has(value)) {
	        return false;
	    }
	    var ids = map.get(value);
	    if (ids.size === 0) {
	        return false;
	    }
	    return true;
	}
	helpers.hasAddSnapValue = hasAddSnapValue;
	function isCloseEqual(a, b, tolerance) {
	    if (tolerance === void 0) { tolerance = 1e-9; }
	    var diff = Math.abs(a - b);
	    return diff <= tolerance;
	}
	helpers.isCloseEqual = isCloseEqual;
	// 用于判断是否为 null 或 undefined
	function isNil(value) {
	    return value == null;
	}
	helpers.isNil = isNil;

	var SnapValueManager$1 = {};

	var __read$1 = (commonjsGlobal && commonjsGlobal.__read) || function (o, n) {
	    var m = typeof Symbol === "function" && o[Symbol.iterator];
	    if (!m) return o;
	    var i = m.call(o), r, ar = [], e;
	    try {
	        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
	    }
	    catch (error) { e = { error: error }; }
	    finally {
	        try {
	            if (r && !r.done && (m = i["return"])) m.call(i);
	        }
	        finally { if (e) throw e.error; }
	    }
	    return ar;
	};
	var __spreadArray$1 = (commonjsGlobal && commonjsGlobal.__spreadArray) || function (to, from, pack) {
	    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
	        if (ar || !(i in from)) {
	            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
	            ar[i] = from[i];
	        }
	    }
	    return to.concat(ar || Array.prototype.slice.call(from));
	};
	Object.defineProperty(SnapValueManager$1, "__esModule", { value: true });
	SnapValueManager$1.SnapValueManager = void 0;
	var helpers_1$1 = helpers;
	var SnapValueManager = /** @class */ (function () {
	    function SnapValueManager(initialValues) {
	        if (initialValues === void 0) { initialValues = []; }
	        this.values = [];
	        // 初始化时对数据进行排序
	        this.values = initialValues.slice().sort(function (a, b) { return a - b; });
	    }
	    Object.defineProperty(SnapValueManager.prototype, "size", {
	        get: function () {
	            return this.values.length;
	        },
	        enumerable: false,
	        configurable: true
	    });
	    SnapValueManager.prototype.add = function (value) {
	        var index = this._findInsertIndex(value);
	        this.values.splice(index, 0, value);
	    };
	    SnapValueManager.prototype.addBatch = function (values) {
	        this.values = this.values.concat(values).sort(function (a, b) { return a - b; });
	    };
	    SnapValueManager.prototype.delete = function (value) {
	        var index = this._binarySearch(value);
	        if (index >= 0) {
	            this.values.splice(index, 1);
	            return true;
	        }
	        return false;
	    };
	    SnapValueManager.prototype.deleteBatch = function (values) {
	        var toDelete = new Set(values);
	        this.values = this.values.filter(function (item) { return !toDelete.has(item); });
	    };
	    SnapValueManager.prototype.has = function (value) {
	        return this._binarySearch(value) >= 0;
	    };
	    SnapValueManager.prototype.getIndexByValue = function (value) {
	        return this._binarySearch(value);
	    };
	    SnapValueManager.prototype.getValueByIndex = function (index) {
	        return this.values[index];
	    };
	    SnapValueManager.prototype.getPrev = function (startValue, startIndex) {
	        var result = {
	            value: null,
	            index: null,
	        };
	        if ((0, helpers_1$1.isNil)(startValue)) {
	            return result;
	        }
	        var index = (0, helpers_1$1.isNil)(startIndex) ? this._findInsertIndex(startValue) : startIndex; // 找到插入位置
	        var prevIndex = index - 1;
	        while (prevIndex >= 0) {
	            var prevValue = this.values[prevIndex];
	            if ((0, helpers_1$1.isCloseEqual)(prevValue, startValue)) {
	                prevIndex--;
	                continue;
	            }
	            result.value = prevValue;
	            result.index = prevIndex;
	            break;
	        }
	        return result;
	    };
	    SnapValueManager.prototype.prevIterator = function (initValue) {
	        var _a;
	        var _this = this;
	        var value = initValue;
	        var index = undefined;
	        return _a = {},
	            _a[Symbol.iterator] = function () {
	                return {
	                    next: function () {
	                        var _a = _this.getPrev(value, index), prevValue = _a.value, prevIndex = _a.index;
	                        if (prevValue === null) {
	                            return {
	                                value: null,
	                                done: true,
	                            };
	                        }
	                        value = prevValue;
	                        index = prevIndex;
	                        return {
	                            value: { value: prevValue, index: prevIndex },
	                            done: false,
	                        };
	                    },
	                };
	            },
	            _a;
	    };
	    SnapValueManager.prototype.getNext = function (startValue, startIndex) {
	        var result = {
	            value: null,
	            index: null,
	        };
	        if ((0, helpers_1$1.isNil)(startValue)) {
	            return result;
	        }
	        var index = (0, helpers_1$1.isNil)(startIndex) ? this._findInsertIndex(startValue) : startIndex; // 找到插入位置
	        var nextIndex = index;
	        while (nextIndex < this.values.length) {
	            var nextValue = this.values[nextIndex];
	            if ((0, helpers_1$1.isCloseEqual)(nextValue, startValue)) {
	                nextIndex++;
	                continue;
	            }
	            result.value = nextValue;
	            result.index = nextIndex;
	            break;
	        }
	        return result;
	    };
	    SnapValueManager.prototype.nextIterator = function (initValue) {
	        var _a;
	        var _this = this;
	        var value = initValue;
	        var index = undefined;
	        return _a = {},
	            _a[Symbol.iterator] = function () {
	                return {
	                    next: function () {
	                        var _a = _this.getNext(value, index), nextValue = _a.value, nextIndex = _a.index;
	                        if (nextValue === null) {
	                            return {
	                                value: null,
	                                done: true,
	                            };
	                        }
	                        value = nextValue;
	                        index = nextIndex;
	                        return {
	                            value: { value: nextValue, index: nextIndex },
	                            done: false,
	                        };
	                    },
	                };
	            },
	            _a;
	    };
	    // 二分查找，返回数据索引（如果存在）
	    SnapValueManager.prototype._binarySearch = function (value) {
	        var left = 0;
	        var right = this.values.length - 1;
	        while (left <= right) {
	            var mid = Math.floor((left + right) / 2);
	            if ((0, helpers_1$1.isCloseEqual)(this.values[mid], value)) {
	                return mid; // 找到数据，返回索引
	            }
	            else if (this.values[mid] < value) {
	                left = mid + 1;
	            }
	            else {
	                right = mid - 1;
	            }
	        }
	        return -1; // 数据不存在
	    };
	    // 查找插入位置，保持有序性
	    SnapValueManager.prototype._findInsertIndex = function (value) {
	        var left = 0;
	        var right = this.values.length;
	        while (left < right) {
	            var mid = Math.floor((left + right) / 2);
	            if (this.values[mid] < value) {
	                left = mid + 1;
	            }
	            else {
	                right = mid;
	            }
	        }
	        return left; // 返回插入位置
	    };
	    /**
	     * 返回去重后的数据集合
	     * @returns
	     */
	    SnapValueManager.prototype.getValues = function () {
	        return Array.from(new Set(this.values));
	    };
	    // 返回当前数据集合
	    SnapValueManager.prototype.getAllValues = function () {
	        return __spreadArray$1([], __read$1(this.values), false);
	    };
	    return SnapValueManager;
	}());
	SnapValueManager$1.SnapValueManager = SnapValueManager;

	var types = {};

	(function (exports) {
		Object.defineProperty(exports, "__esModule", { value: true });
		exports.SnapDirection = void 0;
		(function (SnapDirection) {
		    SnapDirection[SnapDirection["PREV"] = 0] = "PREV";
		    SnapDirection[SnapDirection["NEXT"] = 1] = "NEXT";
		    SnapDirection[SnapDirection["BOTH"] = 2] = "BOTH";
		})(exports.SnapDirection || (exports.SnapDirection = {})); 
	} (types));

	var __assign = (commonjsGlobal && commonjsGlobal.__assign) || function () {
	    __assign = Object.assign || function(t) {
	        for (var s, i = 1, n = arguments.length; i < n; i++) {
	            s = arguments[i];
	            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
	                t[p] = s[p];
	        }
	        return t;
	    };
	    return __assign.apply(this, arguments);
	};
	var __values = (commonjsGlobal && commonjsGlobal.__values) || function(o) {
	    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
	    if (m) return m.call(o);
	    if (o && typeof o.length === "number") return {
	        next: function () {
	            if (o && i >= o.length) o = void 0;
	            return { value: o && o[i++], done: !o };
	        }
	    };
	    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
	};
	var __read = (commonjsGlobal && commonjsGlobal.__read) || function (o, n) {
	    var m = typeof Symbol === "function" && o[Symbol.iterator];
	    if (!m) return o;
	    var i = m.call(o), r, ar = [], e;
	    try {
	        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
	    }
	    catch (error) { e = { error: error }; }
	    finally {
	        try {
	            if (r && !r.done && (m = i["return"])) m.call(i);
	        }
	        finally { if (e) throw e.error; }
	    }
	    return ar;
	};
	var __spreadArray = (commonjsGlobal && commonjsGlobal.__spreadArray) || function (to, from, pack) {
	    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
	        if (ar || !(i in from)) {
	            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
	            ar[i] = from[i];
	        }
	    }
	    return to.concat(ar || Array.prototype.slice.call(from));
	};
	Object.defineProperty(SnapAxis$1, "__esModule", { value: true });
	SnapAxis$1.SnapAxis = void 0;
	var helpers_1 = helpers;
	var SnapValueManager_1 = SnapValueManager$1;
	var types_1 = types;
	var DefaultGetUpdaterOptions = {
	    disableSnap: false,
	    distance: 5,
	    scale: 1,
	};
	var _version = "1.3.0";
	/**
	 * SnapAxis 是一个用于管理吸附轴（如水平轴或垂直轴）的类，支持吸附点的添加、删除、更新以及吸附逻辑的实现。
	 */
	var SnapAxis = /** @class */ (function () {
	    /**
	     * 构造函数，初始化吸附轴。
	     * @param {SnapAxisOptions} options - 配置选项，包含吸附点数组和获取吸附单位值的函数。
	     */
	    function SnapAxis(options) {
	        if (options === void 0) { options = {}; }
	        this._debug = false;
	        /**
	         * 用于管理吸附值的存储和查询
	         */
	        this.snapValueManager = new SnapValueManager_1.SnapValueManager();
	        /**
	         * 用于存储所有吸附点的详细信息，键为吸附点的唯一标识符 id，值为吸附点的完整数据。
	         */
	        this.snapValueMap = new Map();
	        /**
	         * 用于存储每个吸附值对应的吸附点 id 集合。键为吸附值，值为吸附点 id 的集合。
	         */
	        this.snapValueToIdsMap = new Map();
	        /**
	         * 获取吸附单位值的默认实现。
	         * @returns {number} - 吸附单位值
	         */
	        this.__getSnapUnitValue__ = function (scale) {
	            if (scale === void 0) { scale = 1; }
	            return 1 / scale;
	        };
	        var _a = options.snapValues, snapValues = _a === void 0 ? [] : _a, getSnapUnitValue = options.getSnapUnitValue, _b = options.debug, debug = _b === void 0 ? false : _b;
	        this._debug = debug;
	        if (getSnapUnitValue) {
	            this.__getSnapUnitValue__ = getSnapUnitValue;
	        }
	        // 初始化吸附点
	        this._initSnapValues(snapValues);
	    }
	    SnapAxis.fromSnapValues = function (snapValues, options) {
	        if (options === void 0) { options = {}; }
	        return new SnapAxis(__assign(__assign({}, options), { snapValues: snapValues }));
	    };
	    SnapAxis.fromValues = function (values, options) {
	        if (options === void 0) { options = {}; }
	        var radomStr = Math.random().toString(16).substring(2, 6);
	        var snapValues = values.map(function (value, index) { return ({
	            id: "".concat(radomStr, "_").concat(index),
	            value: value,
	        }); });
	        return new SnapAxis(__assign(__assign({}, options), { snapValues: snapValues }));
	    };
	    Object.defineProperty(SnapAxis, "version", {
	        /**
	         * 版本号
	         */
	        get: function () {
	            return _version;
	        },
	        enumerable: false,
	        configurable: true
	    });
	    /**
	     * 安全获取某个吸附值对应的 id 集合。如果不存在，则创建一个新的集合并返回。
	     * @param {number} value - 吸附值
	     * @returns {Set<ISnapValue['id']>} - 吸附值对应的 id 集合
	     */
	    SnapAxis.prototype._safeGetIdsSet = function (value) {
	        var map = this.snapValueToIdsMap;
	        if (!map.has(value)) {
	            var set = new Set();
	            map.set(value, set);
	            return set;
	        }
	        return map.get(value);
	    };
	    /**
	     * 初始化吸附点，将吸附点数据存储到 snapValueMap 和 snapValueToIdsMap 中。
	     * @param {ISnapValue[]} snapValues - 吸附点数组
	     */
	    SnapAxis.prototype._initSnapValues = function (snapValues) {
	        var values = [];
	        for (var i = 0; i < snapValues.length; i++) {
	            var snapValue = snapValues[i];
	            var id = snapValue.id;
	            var value = snapValue.value;
	            if (!Number.isFinite(value) || id == null) {
	                if (this._debug) {
	                    // process.env.NODE_ENV === 'development'
	                    console.warn("[SnapAxis] Invalid snap value:", snapValue);
	                }
	                continue;
	            }
	            values.push(snapValue.value);
	            this.snapValueMap.set(id, snapValue);
	            this._safeGetIdsSet(value).add(id);
	        }
	        this.snapValueManager.addBatch(values);
	    };
	    /**
	     * 获取吸附单位值。
	     * @returns {number} - 吸附单位值
	     */
	    SnapAxis.prototype.getSnapUnitValue = function (scale) {
	        if (scale === void 0) { scale = 1; }
	        var c = this.__getSnapUnitValue__;
	        return c(scale);
	    };
	    /**
	     * 判断是否存在指定 id 的吸附点。
	     * @param {ISnapValue['id']} id - 吸附点 id
	     * @returns {boolean} - 是否存在
	     */
	    SnapAxis.prototype.has = function (id) {
	        return this.snapValueMap.has(id);
	    };
	    /**
	     * 判断是否存在指定值的吸附点。
	     * @param {number} value - 吸附值
	     * @returns {boolean} - 是否存在
	     */
	    SnapAxis.prototype.hasValue = function (value) {
	        return this.snapValueToIdsMap.has(value);
	    };
	    /**
	     * 添加吸附点。
	     * @param {ISnapValue} snapValue - 要添加的吸附点
	     */
	    SnapAxis.prototype.addSnapValue = function (snapValue) {
	        var id = snapValue.id, value = snapValue.value;
	        if (!Number.isFinite(value) || id == null) {
	            if (this._debug) {
	                // process.env.NODE_ENV === 'development'
	                console.warn("[SnapAxis] Invalid snap value:", snapValue);
	            }
	            return;
	        }
	        if (this.snapValueMap.has(id)) {
	            var oldSnapValue = this.snapValueMap.get(id);
	            this.deleteSnapValue(oldSnapValue);
	        }
	        this.snapValueMap.set(id, snapValue);
	        var snapValueToIdsMap = this.snapValueToIdsMap;
	        if (!(0, helpers_1.hasAddSnapValue)(snapValueToIdsMap, value)) {
	            this.snapValueManager.add(value);
	        }
	        this._safeGetIdsSet(value).add(id);
	    };
	    /**
	     * 更新吸附点。
	     * @param {ISnapValue} snapValue - 要更新的吸附点
	     * @returns {boolean} - 是否更新成功
	     */
	    SnapAxis.prototype.updateSnapValue = function (snapValue) {
	        if (!this.snapValueMap.has(snapValue.id)) {
	            return false;
	        }
	        var oldSnapValue = this.snapValueMap.get(snapValue.id);
	        this.deleteSnapValue(oldSnapValue);
	        this.addSnapValue(snapValue);
	        return true;
	    };
	    /**
	     * 批量添加吸附点。
	     * @param {ISnapValue[]} snapValues - 要添加的吸附点数组
	     */
	    SnapAxis.prototype.addSnapValues = function (snapValues) {
	        var _this = this;
	        snapValues.forEach(function (snapValue) { return _this.addSnapValue(snapValue); });
	    };
	    /**
	     * 删除吸附点。
	     * @param {ISnapValue} snapValue - 要删除的吸附点
	     * @returns {boolean} - 是否删除成功
	     */
	    SnapAxis.prototype.deleteSnapValue = function (snapValue) {
	        var id = snapValue.id, value = snapValue.value;
	        this.snapValueMap.delete(id);
	        var idsSet = this.snapValueToIdsMap.get(value);
	        if (idsSet) {
	            idsSet.delete(id);
	            if (idsSet.size === 0) {
	                this.snapValueToIdsMap.delete(value);
	                this.snapValueManager.delete(value);
	            }
	        }
	        return true;
	    };
	    /**
	     * 根据 id 删除吸附点。
	     * @param {ISnapValue['id']} id - 吸附点 id
	     * @returns {boolean} - 是否删除成功
	     */
	    SnapAxis.prototype.deleteSnapValueById = function (id) {
	        var snapValue = this.snapValueMap.get(id);
	        if (!snapValue) {
	            return false;
	        }
	        return this.deleteSnapValue(snapValue);
	    };
	    /**
	     * 批量删除吸附点。
	     * @param {ISnapValue[]} snapValues - 要删除的吸附点数组
	     */
	    SnapAxis.prototype.deleteSnapValues = function (snapValues) {
	        var _this = this;
	        snapValues.forEach(function (snapValue) { return _this.deleteSnapValue(snapValue); });
	    };
	    /**
	     * 根据 id 数组批量删除吸附点。
	     * @param {ISnapValue['id'][]} ids - 吸附点 id 数组
	     */
	    SnapAxis.prototype.deleteSnapValueByIds = function (ids) {
	        var _this = this;
	        ids.forEach(function (id) { return _this.deleteSnapValueById(id); });
	    };
	    /**
	     * 给定轴的某个值，匹配该轴对应的所有吸附点。
	     * @example
	     * const res = snapAxis.snapTo(20, 1, { distance: 10 })
	     * if(snapAxis.checkSnapped(res.value)) {
	     *  console.log('当前匹配到的吸附点', snapAxis.getSnappedValues(res.value))
	     * }
	     * @param {number} value - 吸附值
	     * @returns {ISnapValue[]} - 吸附点数组
	     */
	    SnapAxis.prototype.getSnappedValues = function (value) {
	        // const snappedValues: ISnapValue[] = []
	        var _this = this;
	        // const idsSet = this.snapValueToIdsMap.get(value)
	        // if (!idsSet) {
	        //   return snappedValues
	        // }
	        // idsSet.forEach(id => {
	        //   const snapValue = this.snapValueMap.get(id)
	        //   snappedValues.push(snapValue)
	        // })
	        // return snappedValues
	        var idsSet = this.snapValueToIdsMap.get(value);
	        if (!idsSet) {
	            return [];
	        }
	        return Array.from(idsSet).map(function (id) { return _this.snapValueMap.get(id); });
	    };
	    /**
	     * 获取所有吸附点。
	     * @returns {ISnapValue[]} - 所有吸附点数组
	     */
	    SnapAxis.prototype.getSnapValues = function () {
	        return Array.from(this.snapValueMap.values());
	    };
	    /**
	     * 获取吸附值管理器。
	     * @returns {SnapValueManager} - 吸附值管理器
	     */
	    SnapAxis.prototype.getSnapValueManager = function () {
	        return this.snapValueManager;
	    };
	    /**
	     * 判断是否处于吸附状态。
	     * @param {number} value - 吸附值
	     * @returns {boolean} - 是否处于吸附状态
	     */
	    SnapAxis.prototype.checkSnapped = function (value) {
	        // return this.snapValueManager.has(value)
	        var idsSet = this.snapValueToIdsMap.get(value);
	        if (!idsSet) {
	            return false;
	        }
	        return idsSet.size > 0;
	    };
	    /**
	     * 判断多个值（其中一个）是否处于吸附状态。
	     * @param {number[]} values - 吸附值数组
	     * @returns {boolean} - 是否处于吸附状态
	     */
	    SnapAxis.prototype.checkGroupSnapped = function (values) {
	        for (var i = 0; i < values.length; i++) {
	            if (this.checkSnapped(values[i])) {
	                return true;
	            }
	        }
	        return false;
	    };
	    /**
	     * 根据当前值和偏移量，计算吸附后的目标值。
	     * @param {number} axisValue - 当前坐标轴的值
	     * @param {number} offset - 偏移量
	     * @param {Object} options - 吸附选项
	     * @param {number} options.distance - 吸附距离
	     * @returns {value:number, snapped: boolean} - 吸附后的目标值及状态
	     */
	    SnapAxis.prototype.snapTo = function (axisValue, offset, options) {
	        var e_1, _a;
	        var distance = options.distance, minStep = options.minStep;
	        var unitValue = minStep || this.getSnapUnitValue(1); // 吸附单位值
	        var newSnapValue = axisValue + offset;
	        var targetAxisValue = axisValue + offset;
	        var absOffset = Math.abs(offset);
	        // const halfUnitValue = unitValue * 0.5
	        // const minStepValue = halfUnitValue
	        if ((0, helpers_1.isCloseEqual)(absOffset, 0)) {
	            return { value: axisValue, snapped: false };
	        }
	        if (distance <= 0) {
	            return { value: newSnapValue, snapped: false };
	        }
	        // if (distance < minStepValue) {
	        //   return newSnapValue
	        // }
	        var snapped = absOffset > distance ? false : this.checkSnapped(axisValue); //axis === SnapAxis.X ? this.hasXAxisSnapPoints(axisValue) : this.hasYAxisSnapPoints(axisValue)
	        // if (snapped) {
	        // 移动距离小于等于 distance 时，优先准确吸附
	        // if (offset <= distance) {
	        if (this.checkSnapped(targetAxisValue)) {
	            return { value: targetAxisValue, snapped: true };
	        }
	        // }
	        // }
	        // 判断移动方向
	        var moveLeft = offset < 0;
	        var svm = this.snapValueManager;
	        var startSnapValue = snapped ? axisValue : targetAxisValue;
	        var snapValues = moveLeft ? svm.prevIterator(startSnapValue) : svm.nextIterator(startSnapValue); // 从当前值开始查找
	        var canSnapTo = function (value, distance) {
	            // 用于非吸附状态或者吸附状态下，offset>distance 场景
	            if (!(0, helpers_1.isCloseEqual)(value, targetAxisValue)) {
	                // move left
	                // eg: snapValues=[1, 2, 3, 4, 5] 移动目标 axisValue=8 ->(offset=-4.3) targetAxisValue=3.7 ，应该吸附到 3
	                if (moveLeft && value > targetAxisValue) {
	                    return false;
	                }
	                // move right
	                // eg: snapValues=[1, 2, 3, 4, 5] 移动目标 axisValue=1 ->(offset=3.3) targetAxisValue=4.3 ，应该吸附到 5
	                if (!moveLeft && value < targetAxisValue) {
	                    return false;
	                }
	            }
	            if ((0, helpers_1.isCloseEqual)(value, targetAxisValue, distance)) {
	                return true;
	            }
	            return false;
	        };
	        try {
	            for (var snapValues_1 = __values(snapValues), snapValues_1_1 = snapValues_1.next(); !snapValues_1_1.done; snapValues_1_1 = snapValues_1.next()) {
	                var value = snapValues_1_1.value.value;
	                if (value === null) {
	                    continue;
	                }
	                if (snapped) {
	                    // 当处于吸附状态下试，如果吸附点和初始位置的距离大于 distance，则不会移动
	                    // eg: snapValues=[11, 20, ...]，当前axisValue=10(snapped=true)，offset=4 targetAxisValue=14，此时不会移动，还是10
	                    if (Math.abs(value - axisValue) > distance) {
	                        return { value: axisValue, snapped: false };
	                    }
	                    // 移动距离小于等于单位步长时，吸附到[axisValue + offset - unitValue * 0.25, axisValue+offset+ unitValue* 0.25]之间的值
	                    if (absOffset <= unitValue) {
	                        var a = targetAxisValue - unitValue * 0.25;
	                        var b = targetAxisValue + unitValue * 0.25;
	                        if (moveLeft && value < a) {
	                            return { value: axisValue, snapped: false };
	                        }
	                        if (!moveLeft && value > b) {
	                            return { value: axisValue, snapped: false };
	                        }
	                        if (value >= a && value <= b) {
	                            return { value: value, snapped: true };
	                        }
	                    }
	                    else {
	                        // 移动距离大于单位步长时，吸附距离调整为对应的吸附点到初始点的一半，并执行吸附检测
	                        var d = Math.min(Math.abs(value - axisValue) / 2, distance);
	                        if (canSnapTo(value, d)) {
	                            return { value: value, snapped: true };
	                        }
	                        else {
	                            return { value: axisValue, snapped: false };
	                        }
	                    }
	                }
	                else {
	                    if (canSnapTo(value, distance)) {
	                        return { value: value, snapped: true };
	                    }
	                }
	            }
	        }
	        catch (e_1_1) { e_1 = { error: e_1_1 }; }
	        finally {
	            try {
	                if (snapValues_1_1 && !snapValues_1_1.done && (_a = snapValues_1.return)) _a.call(snapValues_1);
	            }
	            finally { if (e_1) throw e_1.error; }
	        }
	        if (snapped) {
	            if (absOffset <= distance) {
	                return { value: axisValue, snapped: false };
	            }
	        }
	        return { value: targetAxisValue, snapped: false };
	    };
	    /**
	     * 根据给定多个值和偏移量，计算吸附后的目标值。
	     * @param {number[]} axisValues - 当前坐标轴的多值
	     * @param {number} offset - 偏移量
	     * @param {Object} options - 吸附选项
	     * @param {number} options.distance - 吸附距离
	     * @returns {values:number[], snapped: boolean} - 吸附后的目标值及状态
	     */
	    SnapAxis.prototype.snapGroupTo = function (axisValues, offset, options) {
	        if (!axisValues.length) {
	            return {
	                values: [],
	                snapped: false,
	            };
	        }
	        if (axisValues.length === 1) {
	            var result = this.snapTo(axisValues[0], offset, options);
	            return {
	                values: [result.value],
	                snapped: result.snapped,
	            };
	        }
	        // 是否存在吸附点
	        var hasSnapValue = false;
	        // 是否发生了吸附
	        var snapped = false;
	        // 最小偏移量
	        var minOffset = 0;
	        var d1 = Infinity;
	        for (var i = 0; i < axisValues.length; i++) {
	            var value = axisValues[i];
	            var result = this.snapTo(value, offset, options);
	            var d2 = Math.abs(result.value - value);
	            if (!d2) {
	                hasSnapValue = true;
	            }
	            if (result.snapped) {
	                snapped = true;
	                if (d2 < d1) {
	                    d1 = d2;
	                    minOffset = result.value - value;
	                }
	            }
	        }
	        if (!hasSnapValue && !snapped) {
	            return {
	                values: axisValues.map(function (value) { return value + offset; }),
	                snapped: false,
	            };
	        }
	        if (hasSnapValue && !snapped) {
	            return {
	                values: __spreadArray([], __read(axisValues), false),
	                snapped: false,
	            };
	        }
	        if (hasSnapValue && snapped) {
	            return {
	                values: axisValues.map(function (value) { return value + minOffset; }),
	                snapped: true,
	            };
	        }
	        if (!hasSnapValue && snapped) {
	            return {
	                values: axisValues.map(function (value) { return value + minOffset; }),
	                snapped: true,
	            };
	        }
	        return {
	            values: axisValues.map(function (value) { return value + offset; }),
	            snapped: false,
	        };
	    };
	    /**
	     * 吸附到最近的吸附点。
	     * @param value
	     * @param options
	     * @returns
	     */
	    SnapAxis.prototype.snapToNearest = function (value, options) {
	        if (options === void 0) { options = {}; }
	        var _a = options.direction, direction = _a === void 0 ? types_1.SnapDirection.BOTH : _a, distance = options.distance;
	        var result = {
	            snapped: false,
	            value: value,
	        };
	        var prevValue = null;
	        var nextValue = null;
	        var issetDistance = !(0, helpers_1.isNil)(distance) && distance > 0;
	        if (direction === types_1.SnapDirection.BOTH || direction === types_1.SnapDirection.PREV) {
	            var prev = this.snapValueManager.getPrev(value);
	            prevValue = prev.value;
	            if (issetDistance) {
	                prevValue = (0, helpers_1.isCloseEqual)(prevValue, value, distance) ? prevValue : null;
	            }
	        }
	        if (direction === types_1.SnapDirection.BOTH || direction === types_1.SnapDirection.NEXT) {
	            var next = this.snapValueManager.getNext(value);
	            nextValue = next.value;
	            if (issetDistance) {
	                nextValue = (0, helpers_1.isCloseEqual)(nextValue, value, distance) ? nextValue : null;
	            }
	        }
	        if (prevValue === null && nextValue === null) {
	            return result;
	        }
	        if (prevValue === null) {
	            result.snapped = true;
	            result.value = nextValue;
	            return result;
	        }
	        if (nextValue === null) {
	            result.snapped = true;
	            result.value = prevValue;
	            return result;
	        }
	        var d1 = Math.abs(value - prevValue);
	        var d2 = Math.abs(value - nextValue);
	        result.snapped = true;
	        result.value = d1 < d2 ? prevValue : nextValue;
	        return result;
	    };
	    /**
	     * 吸附到最近的吸附点
	     * @param values
	     * @param options
	     * @returns
	     */
	    SnapAxis.prototype.snapGroupToNearest = function (values, options) {
	        if (options === void 0) { options = {}; }
	        var result = {
	            values: values,
	            snapped: false,
	        };
	        if (!values.length) {
	            return result;
	        }
	        var snapped = false;
	        var minOffset = 0;
	        var d1 = Infinity;
	        for (var i = 0; i < values.length; i++) {
	            var value = values[i];
	            var r = this.snapToNearest(value, options);
	            var d2 = Math.abs(r.value - value);
	            if (r.snapped) {
	                snapped = true;
	                if (d2 < d1) {
	                    d1 = d2;
	                    minOffset = r.value - value;
	                }
	            }
	        }
	        if (!snapped) {
	            return result;
	        }
	        return {
	            values: values.map(function (value) { return value + minOffset; }),
	            snapped: true,
	        };
	    };
	    /**
	     * 如果当前值处于非吸附状态，则吸附到最近的吸附点。
	     * @param {number} value - 当前值
	     * @param {string} direction - 吸附方向
	     * @returns {Object} - 吸附结果
	     */
	    SnapAxis.prototype.snapToNearestIfNeeded = function (value, options) {
	        if (this.checkSnapped(value)) {
	            return {
	                snapped: false,
	                value: value,
	            };
	        }
	        return this.snapToNearest(value, options);
	    };
	    /**
	     * 如果当前值（多个）处于非吸附状态，则吸附到最近的吸附点。
	     * @param values
	     * @param options
	     * @returns
	     */
	    SnapAxis.prototype.snapGroupToNearestIfNeeded = function (values, options) {
	        if (this.checkGroupSnapped(values)) {
	            return {
	                snapped: false,
	                values: values,
	            };
	        }
	        return this.snapGroupToNearest(values, options);
	    };
	    /**
	     * 吸附到上一个吸附点。
	     * @param value
	     * @param options
	     * @returns
	     */
	    SnapAxis.prototype.snapToPrev = function (value, options) {
	        if (options === void 0) { options = {}; }
	        return this.snapToNearest(value, __assign(__assign({}, options), { direction: types_1.SnapDirection.PREV }));
	    };
	    /**
	     * 吸附到上一个吸附点。
	     * @param values
	     * @param options
	     * @returns
	     */
	    SnapAxis.prototype.snapGroupToPrev = function (values, options) {
	        if (options === void 0) { options = {}; }
	        return this.snapGroupToNearest(values, __assign(__assign({}, options), { direction: types_1.SnapDirection.PREV }));
	    };
	    /**
	     * 吸附到下一个吸附点。
	     * @param value
	     * @param options
	     * @returns
	     */
	    SnapAxis.prototype.snapToNext = function (value, options) {
	        if (options === void 0) { options = {}; }
	        return this.snapToNearest(value, __assign(__assign({}, options), { direction: types_1.SnapDirection.NEXT }));
	    };
	    /**
	     * 吸附到下一个吸附点。
	     * @param values
	     * @param options
	     * @returns
	     */
	    SnapAxis.prototype.snapGroupToNext = function (values, options) {
	        if (options === void 0) { options = {}; }
	        return this.snapGroupToNearest(values, __assign(__assign({}, options), { direction: types_1.SnapDirection.NEXT }));
	    };
	    /**
	     * 获取吸附更新器。
	     * @param initValues
	     * @param startAxisValue
	     * @param options
	     * @returns
	     */
	    SnapAxis.prototype.getSnapGroupUpdater = function (initValues, startAxisValue, options) {
	        var _this = this;
	        var currentValues = initValues;
	        var noSnapValue = initValues[0];
	        var lastAxisValue = startAxisValue;
	        var opts = options
	            ? __assign(__assign({}, DefaultGetUpdaterOptions), options) : DefaultGetUpdaterOptions;
	        return function (currentAxisValue, options) {
	            var hasSetDelta = false;
	            if (options) {
	                if (options.initValues) {
	                    initValues = options.initValues;
	                    noSnapValue = initValues[0];
	                    currentValues = initValues;
	                }
	                if (!(0, helpers_1.isNil)(options.delta)) {
	                    currentAxisValue = lastAxisValue + options.delta;
	                    hasSetDelta = true;
	                }
	            }
	            if ((0, helpers_1.isCloseEqual)(currentAxisValue, lastAxisValue)) {
	                return { values: currentValues, snapped: false };
	            }
	            if (options) {
	                opts = __assign(__assign({}, opts), options);
	            }
	            var scale = opts.scale || 1;
	            var direction = currentAxisValue > lastAxisValue ? types_1.SnapDirection.NEXT : types_1.SnapDirection.PREV;
	            var delta = hasSetDelta ? options.delta : (currentAxisValue - lastAxisValue) / scale;
	            lastAxisValue = currentAxisValue;
	            if (!initValues.length) {
	                return { values: [], snapped: false };
	            }
	            noSnapValue += delta;
	            var offset = noSnapValue - currentValues[0];
	            if (direction === types_1.SnapDirection.PREV) {
	                if (offset > 0) {
	                    offset = 0;
	                }
	            }
	            else {
	                if (offset < 0) {
	                    offset = 0;
	                }
	            }
	            if (!offset) {
	                return { values: currentValues, snapped: false };
	            }
	            var result = opts.disableSnap
	                ? { values: currentValues.map(function (value) { return value + offset; }), snapped: false }
	                : _this.snapGroupTo(currentValues, offset, {
	                    distance: opts.distance / scale,
	                    minStep: _this.getSnapUnitValue(scale),
	                });
	            currentValues = result.values;
	            return result;
	        };
	    };
	    /**
	     * 获取吸附更新器。
	     * @param initValue 初始值
	     * @param startAxisValue 起始坐标轴值（例如事件的pageX或pageY）
	     * @param options
	     * @returns {Function} - 吸附更新器
	     */
	    SnapAxis.prototype.getSnapUpdater = function (initValue, startAxisValue, options) {
	        var updater = this.getSnapGroupUpdater([initValue], startAxisValue, options);
	        return function (currentAxisValue, options) {
	            var result = updater(currentAxisValue, options && !(0, helpers_1.isNil)(options.initValue)
	                ? __assign(__assign({}, options), { initValues: [options.initValue] }) : options);
	            return {
	                value: result.values[0],
	                snapped: result.snapped,
	            };
	        };
	    };
	    SnapAxis.SnapDirection = {
	        PREV: types_1.SnapDirection.PREV,
	        NEXT: types_1.SnapDirection.NEXT,
	        BOTH: types_1.SnapDirection.BOTH,
	    };
	    SnapAxis.SnapValueManager = SnapValueManager_1.SnapValueManager;
	    return SnapAxis;
	}());
	SnapAxis$1.SnapAxis = SnapAxis;

	Object.defineProperty(umd_exports, "__esModule", { value: true });
	var SnapAxis_1 = SnapAxis$1;
	var _default = umd_exports.default = SnapAxis_1.SnapAxis;

	return _default;

}));
